package likouJava;

import java.util.Stack;

public class 最小栈 {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    //初始化
    public void MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    // 压栈
    public void push(int val) {
        stack.push(val);

        if (minStack.isEmpty() || val <= minStack.peek()){
            minStack.push(val);
        }
    }

    // 出栈
    public void pop() {
        int removed = stack.pop();
        if (removed == minStack.peek()){
            minStack.pop();
        }
    }

    // 获取栈顶元素
    public int top() {
        return stack.peek();
    }

    // 获取最小元素
    public int getMin() {
        return minStack.peek();
    }

}
